require(['grid', 'topBar','layout','select2'], function () {
    var editDialog, $top = $('#top'), $grid = $('#grid'),reloadGridData,carIds;

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	reloadGridData();
                    }
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '+添加驾驶员',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	carIds = commonFun.getLeftTreeCheckedNodes().CAR;
    					if(typeof(carIds) == 'undefined'){ // 左侧树没有选中车辆
    						$.messager.alert("提示","请选中一辆车，进行添加驾驶员。","info");
    					}else if(carIds.length > 1){ // 左侧树选中车辆大于一辆
    						$.messager.alert("提示","您只能选中一辆车，进行添加驾驶员。","info");
    					}else{
    						getEditDialog().dialog('exec', 'getUnBoundDrvPage',carIds[0]);
                    }
                }
            }
        ]
    });
}

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/carDrvRel/searchBoundDrvPage.htm",
            colModel: [
                {name: "relId",label: 'id', key: true, hidden: true},
                {name: "drvId",label: 'drvId',hidden: true},
                {name: "carId",label: 'carId', hidden: true},
                {name: "licPltNo",label: '车牌号', width: 100},
                {name: "eqmtCd",label: '设备号', width: 100},
                {name: "drvName",label: '驾驶员姓名', width: 100},
                {name: "workEmpeEmpno",label: '员工工号', width: 100,align:'right'},
                {name: "timecardCardno",label: '考勤卡卡号', width: 100,align:'right'},
                {name: "gender",label: '性别', width: 50, dict: "SEX00000"},
                {name: "ctcWay",label: '联系方式', width: 100,align:'right'},
                {name: "operate", width: 100, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '解除驾驶员',
                        css:'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', "是否解除驾驶员?", 'delete', function (r) {
                                if (r) {
                                	$.ajaxExt({
            							url : "/carDrvRel/unBindDrv.htm",
            							data : {'relId' : data.relId}
            						}).done(function(){
            							searchCarDriver(carIds);
                                        commonFun.reloadLeftTree();
            						});
                                }
                            });
                        }
                    }]
                }
            ],
            multiselect: false
        });
        
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog() {
    	if(editDialog){
    		editDialog.dialog('open');
    	}else{
            editDialog = $('<div>').dialog({
                title: '添加驾驶员',
                url: './master/setting/sysCarDriverSetEdit.htm',
                height: 550,
                width: 1020,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	
                        	editDialog.dialog('exec', 'add',function(){
                        		editDialog.dialog('close');
                		    	var carStr = commonFun.getLeftTreeCheckedNodes("CAR").toString();
                		    	searchCarDriver(carStr);
                                commonFun.reloadLeftTree();
                			});
                        	
                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
    	}
        return editDialog;
    }
    
	//载入车辆驾驶员绑定
	reloadGridData = function (){
		carIds = commonFun.getLeftTreeCheckedNodes().CAR;
		if(typeof(carIds) == 'undefined'){ // 左侧树没有选中车辆
			$.messager.alert("提示","请在左侧树中选择车辆，再点击查询。","info");
		}else{
			$grid.grid('setGridParam',{page:1,datatype:"json",postData:{carIds:carIds}}).trigger("reloadGrid");
		}
	};
	
	//载入线路车辆绑定
	searchCarDriver = function (cars){
		$grid.grid('setGridParam',{page:1,datatype:"json",postData:{carIds:carIds}}).trigger("reloadGrid");
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			carIds = commonFun.getLeftTreeCheckedNodes().CAR;
			if(typeof(carIds) == 'undefined'){ // 左侧树没有选中车辆
				$.messager.alert("提示","请在左侧树中选择车辆，再点击查询。","info");
			}else{
	    		$.exportFile("/carDrvRel/exportCarDrvRel"+suffix+".htm",{carIds:carIds});
			}
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});